/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.restclient;

import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Daniel
 */
public class HotelTableModel extends AbstractTableModel{
    static final Logger logger = Logger.getLogger(HotelTableModel.class.getName());
    private HotelRESTClient hotelRESTClient = new HotelRESTClient();
    private List<HotelDTO> hotels = new ArrayList<HotelDTO>();
    
    public void addHotel(HotelDTO hotel) throws IllegalOrphanException, PreexistingEntityException{
        loadData();
         
    }
    
       
    public void removeHotel(int row) throws IllegalArgumentException{  
        HotelDTO hotel;
        hotel = hotels.get(row);         
        hotelRESTClient.deleteHotel(hotel);
        fireTableRowsDeleted(row, row);
      
        hotels = hotelRESTClient.findAllHotels();
    }
    
    @Override
    public int getRowCount() {
        return hotels.size();
    }

    public int getColumnCount() {
        return 4;
    }
       
    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Name";
            case 1:
                return "Address";
            case 2:
                return "Description";
            case 3:
                return "City";
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
  
    public Object getValueAt(int rowIndex, int columnIndex) {
        HotelDTO hotel = hotels.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return hotel.getName();
            case 1:
                return hotel.getAdress();
            case 2:
                return hotel.getDescription();
            case 3:
                return hotel.getCity();
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
            case 1:
            case 2:
            case 3:    
                return true;
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        HotelDTO hotel = hotels.get(rowIndex);
        switch (columnIndex) {
            case 0:
                hotel.setName((String) aValue);
                break;
            case 1:
                hotel.setAdress((String) aValue);
                break;
            case 2:
                hotel.setDescription((String) aValue);
                break;
            case 3:
                hotel.setCity((String) aValue);
                break;
            default:
                logger.log(Level.WARNING, "wrong column index");
                throw new IllegalArgumentException("columnIndex");
        }     
            hotelRESTClient.editHotel(hotel);
            loadData();
  
        fireTableCellUpdated(rowIndex, columnIndex);
    }
    
    public HotelDTO getHotel(Long id) {
        return hotelRESTClient.getHotelById(id);
    }
      
    public void loadData(){
            hotels = hotelRESTClient.findAllHotels();  
            if(!hotels.isEmpty())
            {
            int lastRow = hotels.size() - 1;
            fireTableRowsInserted(0, lastRow);
        }
            
           
        }
    
}
